package com.yonyou.crm.base.brand.ctrl;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.crm.base.brand.entity.BrandVO;
import com.yonyou.crm.base.brand.rmitf.IBrandsRmService;
import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.page.entity.Page;
import com.yonyou.crm.common.rest.constant.HttpKeyConstant;
import com.yonyou.crm.common.rest.constant.PCCodeConstant;
import com.yonyou.crm.common.rest.util.RequestUtil;
import com.yonyou.crm.common.rest.util.ResponseUtil;

@RestController
@RequestMapping(value = "/base/brands")
public class BrandsCtrl {

	@Autowired
	private IBrandsRmService service;

	/**
	 * 查询品牌
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(method = RequestMethod.GET)
	public Object list(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);

		@SuppressWarnings("unchecked")
		Page<BrandVO> page = service.getPageList(getRequestPage(paramObj), (Map<String, Object>)paramObj.get("searchMap"));

		return ResponseUtil.getObject(page);
	}

	/**
	 * 品牌详情
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	public Object detail(@PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response)
			throws CrmMessageException {

		if (id == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "id不能为空");
		}

		return ResponseUtil.getObject(service.getDetail(id));
	}

	/**
	 * 新增品牌
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST)
	public Object insert(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		BrandVO brand = RequestUtil.getVO(request, BrandVO.class);

		String validMsg = insertValidator(brand);
		if (!validMsg.equals("")) {
			throw new CrmMessageException(validMsg);
		}

		brand = service.insert(brand);

		if (brand.getId() == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "获取自增主键为空");
		}

		return ResponseUtil.getObject(brand);
	}

	/*
	 * 品牌新增校验
	 * 
	 * @param measure
	 * 
	 * @return String
	 */
	private String insertValidator(BrandVO brand) {

		if (brand == null) {
			return "转换实体失败";
		}

		if (!StringUtils.isNotBlank(brand.getName())) {
			return "品牌名称不能为空";
		}

		return "";
	}

	/**
	 * 修改品牌
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.PUT)
	public Object update(@PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response)
			throws CrmMessageException {

		BrandVO brand = RequestUtil.getVO(request, BrandVO.class);
		if (brand == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "转换实体失败");
		}

		brand.setId(id);

		String validMsg = updateValidator(brand);
		if (!validMsg.equals("")) {
			throw new CrmMessageException(validMsg);
		}

		BrandVO newBrand = service.update(brand);

		return ResponseUtil.getObject(newBrand);
	}

	/*
	 * 品牌编辑校验
	 * 
	 * @param measure
	 * 
	 * @return String
	 */
	private String updateValidator(BrandVO brand) {

		if (brand == null) {
			return "转换实体失败";
		}

		if (!StringUtils.isNotBlank(brand.getName())) {
			return "品牌名称不能为空";
		}

		return "";
	}

	/**
	 * 删除品牌
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
	public Object delete(@PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response)
			throws CrmMessageException {

		if (id == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "id不能为空");
		}

		int successNum = service.delete(id);
		JSONObject json = new JSONObject();
		json.put("flag", successNum);
		return ResponseUtil.getObject(json);
	}

	/**
	 * 批量删除品牌
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "batch", method = RequestMethod.DELETE)
	public Object batchDelete(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		JSONObject paramObj = RequestUtil.getParamObject(request);
		String idsStr = paramObj.getString("ids");

		if (idsStr == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "ids参数为空");
		}

		String[] idArray = idsStr.split(",");

		if (idArray.length == 0) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "至少选中一条记录");
		}

		String message = service.batchDelete(idArray);
		
		Page<BrandVO> page = service.getPageList(getRequestPage(paramObj), (Map<String, Object>)paramObj.get("searchMap"));

		return ResponseUtil.getObject(page, PCCodeConstant.Success, message);
	}

	/**
	 * 停启用计量单位
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "{id}/state", method = RequestMethod.PUT)
	public Object updateEnableState(@PathVariable("id") Long id, HttpServletRequest request,
			HttpServletResponse response) throws CrmMessageException {

		if (id == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "id不能为空");
		}

		Integer enableState = (Integer) RequestUtil.getObject(request, "enableState");

		if (enableState == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "停启用状态不能为空");
		}

		return service.updateEnableState(id, enableState);
	}

	/**
	 * 批量停启用计量单位
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "state", method = RequestMethod.PUT)
	public Object batchUpdateEnableState(HttpServletRequest request, HttpServletResponse response)
			throws CrmMessageException {

		JSONObject paramObj = RequestUtil.getParamObject(request);
		String idsStr = paramObj.getString("ids");

		if (idsStr == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "ids参数为空");
		}

		String[] idArray = idsStr.split(",");

		if (idArray.length == 0) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "至少选中一条记录");
		}

		Integer enableState = paramObj.getInt("enableState");

		if (enableState == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "停启用状态不能为空");
		}

		Page<BrandVO> page = service.batchUpdateEnableState(idArray, enableState, getRequestPage(paramObj), (Map<String, Object>)paramObj.get("searchMap"));

		return ResponseUtil.getObject(page);
	}

	/**
	 * 参照列表
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "ref", method = RequestMethod.GET)
	public Object getRefList(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		
		JSONObject paramObj = RequestUtil.getParamObject(request);
		
		if (paramObj != null) {
			//有分页信息返回分页列表
			if (paramObj.containsKey(HttpKeyConstant.Page) && paramObj.containsKey(HttpKeyConstant.PageSize)) {
				return ResponseUtil.getObject(service.getRefPage(getRequestPage(paramObj), null));
			}
		}
		
		List<BrandVO> voList = service.getRefList(null);
		
		Page<BrandVO> page = new Page<BrandVO>();
		page.setData(voList);
		
		return ResponseUtil.getObject(page);
	}
	
	/**
	 * 获取分页信息
	 * @param paramObj
	 * @return Page<BrandVO>
	 */
	private Page<BrandVO> getRequestPage(JSONObject paramObj) throws CrmMessageException {

		Integer page = (Integer)paramObj.get(HttpKeyConstant.Page);
		Integer pageSize = (Integer)paramObj.get(HttpKeyConstant.PageSize);

		if (page == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "page不能为空");
		}

		if (pageSize == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "pageSize不能为空");
		}

		return new Page<BrandVO>(page, pageSize);
	}

}
